﻿using Nutrapp.DBAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Threading;

namespace Nutrapp.Controllers
{
    //[Authorize]
    public class CategoryController : Controller
    {
        //
        // GET: /Category/
        private NutrappDataContext context = new NutrappDataContext();

        public ActionResult Create()
        {
            var model = new Ingredients_Category();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Ingredients_Category newCategory)
        {
            Session["nameError"] = "";
            Session["existingCategoryError"] = "";

            bool exist = false;
            List<String> categoryNameList = context.Ingredients_Categories.Select(c => c.Name).ToList();

            foreach (String categoryName in categoryNameList)
            {
                if (categoryName == newCategory.Name)
                {
                    exist = true;
                    Session["existingCategoryError"] = "This category already exists. Please, insert correct one.";
                }
            }

            if (newCategory.Name == null)
            {
                Session["nameError"] = "Please, insert correct name. It must be between 3-15 letters. No digits are allowed.";
            }
            else
            {
                var regexItem = new Regex("^([a-zA-Z]+ )*[a-zA-Z]+$");

                if (!regexItem.IsMatch(newCategory.Name) || newCategory.Name.Length < 3 || newCategory.Name.Length > 15)
                {
                    Session["nameError"] = "Please, insert correct name. It must be between 3-15 letters. No digits are allowed.";
                }
            }

            if (exist == false && Session["nameError"].ToString() == "" && Session["existingCategoryError"].ToString() == "")
            {
                context.Ingredients_Categories.InsertOnSubmit(newCategory);
                context.SubmitChanges();
                //TempData["notice"] = "Category saved.";
                //ViewBag.Message = "aaaaaaa";
                //Response.Write("<script>alert('Hello this is alert for mvc')</script>");
                return RedirectToAction("Index", "Ingredients");
            }
            else
            {
                ViewBag.ID = new SelectList(context.Ingredients_Categories, "ID", "Name");
                return View(newCategory);
            }
        }


        [HttpPost]
        [HandleError]
        public ActionResult Delete(Ingredients_Category category)
        {
            try
            {
                using (NutrappDataContext context = new NutrappDataContext())
                {
                    Ingredients_Category ingrCategory = (Ingredients_Category)context.Ingredients_Categories.Where(cat => cat.ID == category.ID).FirstOrDefault();
                    context.Ingredients_Categories.DeleteOnSubmit(ingrCategory);
                    context.SubmitChanges();

                }
            }
            catch (Exception e)
            {

                Session["deleteError"] = "Category is not deleted.";
            }

            return RedirectToAction("Index", "Ingredients");
        }

        [HttpGet]
        public ActionResult Delete()
        {
            return View();
        }

    }
}
